<template>
  <div class="login">
    <TopHeader />
    <el-card class="login-box">
      <div class="logo">
        <img src="/assets/logo.jpeg" alt="Logo">
      </div>
      <h2>用户登录</h2>
      <el-form 
        ref="formRef"
        :model="loginForm"
        :rules="rules"
        @submit.prevent="handleLogin"
        @keydown.enter.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            class="login-btn" 
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
        <div class="register-link">
          还没有账号？<el-button type="primary" link @click="goToRegister">立即注册</el-button>
        </div>
      </el-form>
      <div style="text-align:center;margin-top:20px;">
        <el-button type="success" @click="loginWithGithub">使用GitHub登录</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import TopHeader from '../components/TopHeader.vue'
import { login } from '@/api/auth'
import { useUserStore } from '../stores/user'
const userStore = useUserStore()

const router = useRouter()
const formRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名长度不能小于3个字符', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9_\u4e00-\u9fa5]+$/, message: '用户名只能包含字母、数字、下划线和中文', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6个字符', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9_@#$%^&*.]+$/, message: '密码只能包含字母、数字和特殊字符(@#$%^&*)', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!formRef.value) return
  try {
    await formRef.value.validate()
    loading.value = true
    const res = await login(loginForm)
    if (res.code === 200) {
      ElMessage.success('登录成功')
      console.log( res)
      userStore.setUserInfo(res.data)
      router.push('/dynamicmenu')
    } else {
      ElMessage.error(res.message || '登录失败')
    }
  } catch (error) {
    console.error('表单验证失败：', error)
  } finally {
    loading.value = false
  }
}

const goToRegister = () => {
  router.push({
    path: '/lz/study/register',
    query: {
      from: 'home',
      timestamp: new Date().getTime()
    },
    hash: '#register-form'
  })
}

// GitHub 登录方法
const loginWithGithub = () => {
  // 跳转到后端 OAuth2 授权地址
  window.location.href = 'http://localhost:8080/oauth2/authorization/github'
}
</script>

<style scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f0f2f5 0%, #e6e9f0 100%);
  padding-top: 60px;
}

.login-box {
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(31, 38, 135, 0.08);
  backdrop-filter: blur(10px);
  transition: transform 0.3s ease;
}

.login-box:hover {
  transform: translateY(-5px);
}

h2 {
  text-align: center;
  margin-bottom: 35px;
  color: var(--el-text-color-primary);
  font-size: 28px;
  font-weight: 600;
}

.logo {
  text-align: center;
  margin-bottom: 20px;
}

.logo img {
  width: 80px;
  height: 80px;
  object-fit: contain;
}

.login-btn {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  font-weight: 600;
}

.register-link {
  text-align: center;
  margin-top: 25px;
  font-size: 15px;
  color: var(--el-text-color-regular);
}

:deep(.el-input__wrapper) {
  padding: 8px 12px;
}

:deep(.el-form-item__error) {
  padding-top: 4px;
}
</style> 